package view;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractCellEditor;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableCellEditor;

public class CourseNameCellEditor extends AbstractCellEditor
implements TableCellEditor{
	private String currentText;
	private JTextField textField;
	private JPanel editPanel;
	private Pattern p;
	private Matcher m;
	
	private static final long serialVersionUID = 8253178634956247331L;
	public CourseNameCellEditor(){
		editPanel = new JPanel();    	
    	textField = new JTextField(currentText);
    	editPanel.add(textField);
    	textField.setColumns(20);
    	textField.getDocument().addDocumentListener(new DocumentListener(){

			@Override
			public void changedUpdate(DocumentEvent arg0) {
								
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				currentText = textField.getText();
			}

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				currentText = textField.getText();
			}
    		
    	});    	
    	JButton btnDetails = new JButton("Details ...");
    	btnDetails.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent arg0) {
    			
    		}
    	});
    	editPanel.add(btnDetails);
    	
    	GroupLayout groupLayout = new GroupLayout(editPanel);
    	groupLayout.setHorizontalGroup(
    		groupLayout.createParallelGroup(Alignment.LEADING)
    			.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
    				.addGap(5)
    				.addComponent(textField, GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
    				.addPreferredGap(ComponentPlacement.RELATED)
    				.addComponent(btnDetails)
    				.addContainerGap())
    	);
    	groupLayout.setVerticalGroup(
    		groupLayout.createParallelGroup(Alignment.LEADING)
    			.addGroup(groupLayout.createSequentialGroup()
    				.addGap(5)
    				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
    					.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    					.addComponent(btnDetails))
    				.addGap(1))
    	);
    	editPanel.setLayout(groupLayout);

    	
	}
	@Override
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return currentText;
	}

	@Override
	public Component getTableCellEditorComponent(JTable arg0, Object arg1,
			boolean arg2, int arg3, int arg4) {
		// TODO Auto-generated method stub
		currentText = (String) arg1;
		textField.setText(currentText);
		return editPanel;
	}

}
